/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.instrument.attachment;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.JOptionPane;

import twaver.Element;
import twaver.TWaverConst;
import twaver.network.ui.ElementUI;
import twaver.network.ui.IconAttachment;

class KPIIcon implements Icon{
	private Element element;
	
	public KPIIcon(ElementUI ui){
		this.element = ui.getElement();
	}
	
	public int getIconHeight() {
		return (int)KPIAttachment.HEIGHT;
	}
	
	public int getIconWidth() {
		return (int)KPIAttachment.WIDTH;
	}
	
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D g2d = (Graphics2D)g;
    	final int count = KPIAttachment.KEYS.length;

    	final double width = KPIAttachment.WIDTH / count;
    	for(int i=0; i<count; i++){
    		double proportion = 0;
    		String valueText = (String)element.getClientProperty(KPIAttachment.KEYS[i]);
    		if(valueText != null){
    			proportion = Double.parseDouble(valueText.substring(KPIAttachment.KEYS[i].length() + 1));
    		}
    		g2d.setColor(KPIAttachment.COLORS[i]);
    		g2d.fillRect((int)(x + i * width), 
    					(int)(y + KPIAttachment.HEIGHT * (1 - proportion)), 
    					(int)width, (int)(KPIAttachment.HEIGHT * proportion));
    	}

    	g2d.setColor(Color.GRAY);
    	g2d.setStroke(TWaverConst.BASIC_STROKE);
    	g2d.drawRect(x, y-1, (int)KPIAttachment.WIDTH, (int)KPIAttachment.HEIGHT);
		
	}
}

public class KPIAttachment extends IconAttachment {

	public final static double WIDTH = 20;
	public final static double HEIGHT = 25;

	public final static String[] KEYS = new String[]{"PM-01", "PM-02", "PM-03", "PM-04", "PM-05"};
	public final static Color[] COLORS = new Color[]{Color.GREEN, Color.BLUE, Color.YELLOW, Color.RED, Color.CYAN};

    public KPIAttachment(String name, ElementUI ui) {
        super(name, ui, new KPIIcon(ui));
    }
    
    public void performAction(int gesture, MouseEvent e) {
    	if(TWaverConst.MOUSE_LEFT_DOUBLE_CLICKED == gesture){
        	JOptionPane.showMessageDialog(this.getElementUI().getNetwork(), "KPIAttachment is double clicked");
    	}
    }
};